D:\a\tools.proto\tools.proto\compiler\src\gen\rust\message\write.rs
Line | Count | Source |
1 | | // Copyright (c) 2025, BlockProject 3D |
2 | | // |
3 | | // All rights reserved. |
4 | | // |
5 | | // Redistribution and use in source and binary forms, with or without modification, |
6 | | // are permitted provided that the following conditions are met: |
7 | | // |
8 | | // * Redistributions of source code must retain the above copyright notice, |
9 | | // this list of conditions and the following disclaimer. |
10 | | // * Redistributions in binary form must reproduce the above copyright notice, |
11 | | // this list of conditions and the following disclaimer in the documentation |
12 | | // and/or other materials provided with the distribution. |
13 | | // * Neither the name of BlockProject 3D nor the names of its contributors |
14 | | // may be used to endorse or promote products derived from this software |
15 | | // without specific prior written permission. |
16 | | // |
17 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
19 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
20 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
21 | | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 | | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
23 | | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
24 | | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
25 | | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
26 | | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | |
29 | | use crate::compiler::message::Message; |
30 | | use crate::compiler::util::types::TypePathMap; |
31 | | use crate::gen::base::map::{DefaultTypeMapper, TypePathMapper}; |
32 | | use crate::gen::base::message::Templates; |
33 | | use crate::gen::base::message_write::generate; |
34 | | use crate::gen::base::Error; |
35 | | use crate::gen::rust::util::{gen_where_clause, Lifetime, RustUtils}; |
36 | | use crate::gen::template::Template; |
37 | | use crate::gen::{codec::CodecMap, RustParams}; |
38 | | use itertools::Itertools; |
39 | | |
40 | | const TEMPLATE: &[u8] = include_bytes!("write.template"); |
41 | | |
42 | 45 | fn _gen_message_write_impl( |
43 | 45 | msg: &Message, |
44 | 45 | codec_map: &CodecMap, |
45 | 45 | type_path_map: &TypePathMapper<DefaultTypeMapper>, |
46 | 45 | generics: &str, |
47 | 45 | impl_generics: &str, |
48 | 45 | function: &str, |
49 | 45 | ) -> Result<String, Error> { |
50 | 45 | let mut templates = Templates { |
51 | 45 | template: Template::compile(TEMPLATE).unwrap(), |
52 | 45 | codec_map, |
53 | 45 | }; |
54 | 45 | let where_clauses = msg |
55 | 45 | .fields |
56 | 45 | .iter() |
57 | 91 | .map45 (|field| gen_where_clause(&templates.template, field, type_path_map, function)) |
58 | 45 | .join(""); |
59 | 45 | templates.template.var("where_clauses", where_clauses); |
60 | 45 | if generics.is_empty() { Branch (60:8): [True: 35, False: 10]
Branch (60:8): [Folded - Ignored]
|
61 | 35 | templates.template.var("impl_generics", "").var("generics", "<'_>"); |
62 | 35 | } else { |
63 | 10 | templates.template.var("generics", generics).var("impl_generics", impl_generics); |
64 | 10 | } |
65 | 45 | generate::<RustUtils, _>(templates, msg, type_path_map, function) |
66 | 45 | } |
67 | | |
68 | 33 | pub fn gen_message_write_impl( |
69 | 33 | msg: &Message, |
70 | 33 | codec_map: &CodecMap, |
71 | 33 | type_path_map: &TypePathMap, |
72 | 33 | params: &RustParams, |
73 | 33 | ) -> Result<String, Error> { |
74 | 33 | if msg.ty.is_some() { Branch (74:8): [True: 2, False: 31]
Branch (74:8): [Folded - Ignored]
|
75 | 2 | return Ok(String::new()); |
76 | 31 | } |
77 | 31 | let type_path_map = TypePathMapper::new(type_path_map, DefaultTypeMapper); |
78 | 31 | let generics = RustUtils::get_generics_for_write(msg, &type_path_map, Lifetime::Named).into_string(); |
79 | 31 | let mut code = _gen_message_write_impl(msg, codec_map, &type_path_map, &generics, &generics, "impl")?0 ; |
80 | 55 | if msg.fields.iter()31 .any31 (|v| v.header.is_some()) { Branch (80:8): [True: 6, False: 25]
Branch (80:8): [Folded - Ignored]
|
81 | 6 | let shape_generics = RustUtils::get_generics_for_write(msg, &type_path_map, Lifetime::Anonymous).into_string(); |
82 | 6 | let impl_shape_generics = RustUtils::get_generics_for_write(msg, &type_path_map, Lifetime::None).into_string(); |
83 | 6 | code += &_gen_message_write_impl( |
84 | 6 | msg, |
85 | 6 | codec_map, |
86 | 6 | &type_path_map, |
87 | 6 | &shape_generics, |
88 | 6 | &impl_shape_generics, |
89 | 6 | "impl_shape_write", |
90 | 0 | )?; |
91 | 25 | } |
92 | 31 | if params.enable_write_async { Branch (92:8): [True: 6, False: 25]
Branch (92:8): [Folded - Ignored]
|
93 | 6 | code += &_gen_message_write_impl(msg, codec_map, &type_path_map, &generics, &generics, "impl_async")?0 ; |
94 | 11 | if msg.fields.iter()6 .any6 (|v| v.header.is_some()) { Branch (94:12): [True: 2, False: 4]
Branch (94:12): [Folded - Ignored]
|
95 | 2 | code += &_gen_message_write_impl( |
96 | 2 | msg, |
97 | 2 | codec_map, |
98 | 2 | &type_path_map, |
99 | 2 | &generics, |
100 | 2 | &generics, |
101 | 2 | "impl_shape_write_async", |
102 | 0 | )?; |
103 | 4 | } |
104 | 25 | } |
105 | 31 | Ok(code) |
106 | 33 | } |